<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Book;

class BookController extends Controller
{
    // 列表
    public function list(){
        $books = Book::orderBy('id','desc')->paginate(5);
        return $books;
    }

    // 添加;
    public function add(Request $request){

        $book = new Book();
        $book->bname = $request->bname;
        $book->save();
        return response()->json(['meta'=>['status'=>200,'msg'=>'图书发布成功'],'data'=>$book]);
       
    }

     // 创建
     public function create(Request $request){

        //return $request->getContent();

        $book = new Book();
        $book->bname = $request->bname;
        $book->save();
        return response()->json(['meta'=>['status'=>200,'msg'=>'图书发布成功'],'data'=>$book]);
       
    }
    
    // 更新
    public function update(Request $request){

        $book = Book::find($request->id);
        $book->bname = $request->bname;
        $book->save();
        return response()->json(['meta'=>['status'=>200,'msg'=>'图书更新成功'],'data'=>$book]);
    }
    
    // 删除
    public function del(Request $request){

        $book = Book::find($request->id);
        $book->delete();
        return response()->json(['meta'=>['status'=>200,'msg'=>'图书删除成功'],'data'=>$book]);
        
    }
   
    // 判断是否存在
    public function is_exist(Request $request){
       $is_exist = (bool) Book::find($request->id);
      
       if($is_exist){
          return response()->json(['msg'=>'存在','is_exist'=>true]);
       }else{
         return response()->json(['msg'=>'不存在','is_exist'=>false]);
       }
       
    }

   
}
